<?php
require_once dirname(__FILE__) . '/Config.inc.php';

/**
 *
 * @author Kinmeic
 *        
 */
class Soap {
	public $client;
	
	/**
	 * @param string $uri
	 */
	function __construct($uri) {
		global $Config;
		
		if (isset($Config) && isset($Config['soap'])) {
			$options = array(
					'location' => $Config['soap']['location'] . $uri,
					'uri' => $uri,
					'trace' => TRUE,
					'exceptions' => 1
					);
			$header = new SoapHeader('soap', 'auth', $Config['soap']['auth'], false, SOAP_ACTOR_NEXT);
			
			$this->client = new SoapClient(null, $options);
			$this->client->__setSoapHeaders(array($header));
		} else {
			exit('Fatal error: SOAP configuration does not found in Config.inc.php.');
		}
	}
	
	/**
	 * @param string $function_name
	 * @param mixed $arguments
	 * @return Ambigous <mixed, multitype:number NULL >
	 */
	function __call($function_name, $arguments){
		return $this->call($function_name, $arguments);
	}
	
	/**
	 * @param string $function_name
	 * @param mixed $arguments
	 * @return mixed|multitype:number NULL 
	 */
	private function call($function_name, $arguments) {
		try {
			return unserialize($this->client->__call($function_name, $arguments));
		} catch (SoapFault $e) {
			return array(
					'code' => -1,
					'message' => $e->getMessage()
			);
		}
	}
}

?>